--[[

reputation.lua

Reputation-based stuff.

]]

-- Function: Respond to the whisper about reputation
function LXP_WhisperRep(orgWhisper, recipient)
	local findFaction = nil;

	if (string.len(orgWhisper) > (string.len(LXP_WHISPER_REP))) then
		findFaction = string.sub(orgWhisper, (string.len(LXP_WHISPER_REP) + 2));
		findFaction = string.lower(findFaction);
	end

	local repCur, repMax, repRemain, repPer;

	SendChatMessage(LXP_MSG_RETURNREP, "WHISPER", GetDefaultLanguage("player"), recipient);
	
	for factionIndex = 1, GetNumFactions(), 1 do
		myMessage = nil;
		local name, _, standingID, barMin, barMax, barValue, _, _, isHeader, _ = GetFactionInfo(factionIndex);
		if (not isHeader) then
			barMax	= barMax - barMin;
			barValue	= barValue - barMin;
			barMin	= 0;

			repLeft	= barMax - barValue;
			repPer	= LXP_SF_ValueToPrecision(((barValue/barMax) * 100), 2);
			repRankCur	= (getglobal("FACTION_STANDING_LABEL"..standingID));
			if (standingID < 8) then
				repRankNext	= (getglobal("FACTION_STANDING_LABEL"..(standingID + 1)));
			else
				repRankNext	= LXP_MSG_REPMAX;
			end

			myMessage = format(LXP_MSG_REPLINE, name, barValue, barMax, repPer, repRankCur, repLeft, repRankNext);

			-- Display only the matching factions if search string given
			if (findFaction ~= nil) then
				if (string.find(string.lower(name), findFaction) ~= nil) then
					SendChatMessage(myMessage, "WHISPER", GetDefaultLanguage("player"), recipient);
				end
			else
				SendChatMessage(myMessage, "WHISPER", GetDefaultLanguage("player"), recipient);
			end
		end
	end
end

-- Function: Display the reputation numbers in the chatframe.
function LXP_RepDetails(displayChange)
	if (not LXP_RepValues) then
		LXP_RepValues = {};
	end

	local myFrame = getglobal("ChatFrame2");

	local repDifference;
	local repRemains;
	local repRepeats;
	local repRank;
	local nextID;

	for factionIndex=1, GetNumFactions(), 1 do
		local name, _, standingID, barMin, barMax, barValue, _, _, isHeader, _ = GetFactionInfo(factionIndex);
		if (not isHeader) then

			if (LXP_RepValues[name]) then
				local repDifference = (barValue - LXP_RepValues[name].value);
				local changeDirection;
				nextID = standingID;

				if (repDifference > 0) then
					nextID		= standingID + 1;
					repRemains	= barMax - barValue;
					changeDirection	= " gained";
				elseif (repDifference < 0) then
					repDifference	= abs(repDifference);
					nextID		= standingID - 1;
					repRemains	= barValue - barMin;
					changeDirection	= " lost";
				end

				if (((nextID > 0) and (nextID < 9)) and (nextID ~= standingID)) then
					repRepeats	= repRemains / repDifference;
					repRank	= getglobal("FACTION_STANDING_LABEL"..nextID);

					if (repRepeats > floor(repRepeats)) then
						repRepeats = ceil(repRepeats);
					end
					if (displayChange) then
						myFrame:AddMessage(format(LXP_MSG_REPREPEATS, repRepeats, repRank, name, repDifference..changeDirection, repRemains), 0.5, 0.5, 1.0);
					end
				end
			end

			LXP_RepValues[name]		= {};
			LXP_RepValues[name].ranking	= standingID;
			LXP_RepValues[name].value	= barValue;
		end
	end
end
